<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.1.1">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/avatar.jpg" color="#222">
  <meta name="msvalidate.01" content="4AF2328B2035748153A041B7EEF3D15C">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC:300,300italic,400,400italic,700,700italic%7CLobster:300,300italic,400,400italic,700,700italic%7CPingFang+SC:300,300italic,400,400italic,700,700italic%7CRoboto+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ethanliu6.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":true,"sidebar":{"position":"left","display":"always","padding":12,"offset":18},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"buttons","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":6,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="JavaSE进阶——Day08 该篇主要讲解Java中的异常、Lambda表达式、Stream流、File类、递归">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaSE进阶——Day08 异常、Lambda表达式、Stream流、File类、递归">
<meta property="og:url" content="http://ethanliu6.github.io/2024/04/25/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day08/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day08-%E5%BC%82%E5%B8%B8,Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F,Stream%E6%B5%81%20/index.html">
<meta property="og:site_name" content="&lt;font color&#x3D;&#39;white&#39; &gt;Ethan の Blog&lt;&#x2F;font&gt;">
<meta property="og:description" content="JavaSE进阶——Day08 该篇主要讲解Java中的异常、Lambda表达式、Stream流、File类、递归">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240425014443078.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240425004947611.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240425161803932-20240425162136434.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240426142645400.png">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240427104636356.png">
<meta property="article:published_time" content="2024-04-24T17:36:57.867Z">
<meta property="article:modified_time" content="2024-04-27T02:55:51.379Z">
<meta property="article:author" content="Ethan Liu">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Java进阶">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240425014443078.png">


<link rel="canonical" href="http://ethanliu6.github.io/2024/04/25/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day08/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day08-%E5%BC%82%E5%B8%B8,Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F,Stream%E6%B5%81%20/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://ethanliu6.github.io/2024/04/25/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day08/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day08-%E5%BC%82%E5%B8%B8,Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F,Stream%E6%B5%81%20/","path":"2024/04/25/JavaNotes/JavaSE进阶/day08/JavaSE进阶——Day08-异常,Lambda表达式,Stream流 /","title":"JavaSE进阶——Day08 异常、Lambda表达式、Stream流、File类、递归"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaSE进阶——Day08 异常、Lambda表达式、Stream流、File类、递归 | <font color='white' >Ethan の Blog</font></title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title"><font color='white' >Ethan の Blog</font></p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description"><font color='#FDABC1' size=3>清醒的停留，胜过——盲目的前行</font></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于我"><a href="/aboutme/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">18</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">14</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">53</span></a></li><li class="menu-item menu-item-love"><a href="/categories/Love/" rel="section"><i class="fas fa-heart fa-fw"></i>Love</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9E%E9%A1%BE"><span class="nav-number">1.</span> <span class="nav-text">回顾</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#logback%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4"><span class="nav-number">1.1.</span> <span class="nav-text">logback使用步骤：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#logback%E6%97%A5%E5%BF%97%E7%BA%A7%E5%88%AB"><span class="nav-number">1.2.</span> <span class="nav-text">logback日志级别：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%82%E5%B8%B8"><span class="nav-number">2.</span> <span class="nav-text">异常</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%BC%82%E5%B8%B8"><span class="nav-number">2.1.</span> <span class="nav-text">什么是异常？</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%82%E5%B8%B8%E4%BD%93%E7%B3%BB"><span class="nav-number">2.2.</span> <span class="nav-text">异常体系</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86%E7%9A%84%E6%96%B9%E5%BC%8F"><span class="nav-number">2.3.</span> <span class="nav-text">异常处理的方式：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8"><span class="nav-number">2.4.</span> <span class="nav-text">自定义异常：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#lambda"><span class="nav-number">3.</span> <span class="nav-text">Lambda</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#lambda%E4%BD%9C%E7%94%A8"><span class="nav-number">3.1.</span> <span class="nav-text">lambda作用：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-number">3.2.</span> <span class="nav-text">lambda表达式：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E7%9C%81%E7%95%A5%E6%A8%A1%E5%BC%8F"><span class="nav-number">3.3.</span> <span class="nav-text">Lambda表达式的省略模式：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#stream%E6%B5%81"><span class="nav-number">4.</span> <span class="nav-text">Stream流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#stream%E6%B5%81%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number">4.1.</span> <span class="nav-text">stream流的作用：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#stream%E6%B5%81%E6%96%B9%E6%B3%95%E5%88%86%E7%B1%BB"><span class="nav-number">4.2.</span> <span class="nav-text">Stream流方法分类：</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96stream%E6%B5%81%E5%AF%B9%E8%B1%A1"><span class="nav-number">4.2.1.</span> <span class="nav-text">获取Stream流对象：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%AD%E9%97%B4%E6%96%B9%E6%B3%95"><span class="nav-number">4.2.2.</span> <span class="nav-text">中间方法：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%88%E7%BB%93%E6%96%B9%E6%B3%95"><span class="nav-number">4.2.3.</span> <span class="nav-text">终结方法：</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#stream%E6%B5%81%E8%BD%AC%E6%8D%A2"><span class="nav-number">4.3.</span> <span class="nav-text">Stream流转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#stream%E6%B5%81%E6%94%B6%E9%9B%86%E6%96%B9%E6%B3%95"><span class="nav-number">4.4.</span> <span class="nav-text">Stream流收集方法</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#optional%E7%B1%BB%E5%88%A4%E7%A9%BA"><span class="nav-number">5.</span> <span class="nav-text">Optional类判空</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#file%E7%B1%BB"><span class="nav-number">6.</span> <span class="nav-text">File类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#file%E7%B1%BB%E7%9A%84%E4%BB%8B%E7%BB%8D"><span class="nav-number">6.1.</span> <span class="nav-text">File类的介绍</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="nav-number">6.2.</span> <span class="nav-text">构造方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#file%E7%B1%BB%E7%9A%84%E5%88%9B%E5%BB%BA%E5%8A%9F%E8%83%BD"><span class="nav-number">6.3.</span> <span class="nav-text">File类的创建功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#file%E7%B1%BB%E7%9A%84%E5%88%A0%E9%99%A4%E5%8A%9F%E8%83%BD"><span class="nav-number">6.4.</span> <span class="nav-text">File类的删除功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#file%E7%B1%BB%E7%9A%84%E5%88%A4%E6%96%AD%E5%92%8C%E8%8E%B7%E5%8F%96%E5%8A%9F%E8%83%BD"><span class="nav-number">6.5.</span> <span class="nav-text">File类的判断和获取功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#file%E7%B1%BB%E9%AB%98%E7%BA%A7%E8%8E%B7%E5%8F%96%E5%8A%9F%E8%83%BD"><span class="nav-number">6.6.</span> <span class="nav-text">File类高级获取功能</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0"><span class="nav-number">6.6.1.</span> <span class="nav-text">练习 :</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%92%E5%BD%92"><span class="nav-number">7.</span> <span class="nav-text">递归</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A6%82%E8%BF%B0"><span class="nav-number">7.1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A5%BD%E5%A4%84"><span class="nav-number">7.2.</span> <span class="nav-text">好处</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F"><span class="nav-number">7.3.</span> <span class="nav-text">注意</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B"><span class="nav-number">7.4.</span> <span class="nav-text">案例</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Ethan Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Ethan Liu</p>
  <div class="site-description" itemprop="description"><font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2V0aGFubGl1Ng==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ethanliu6"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmxpdWUyMzM2QGdtYWlsLmNvbQ==" title="E-Mail → mailto:liue2336@gmail.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTMyNzA5OTk3Nw==" title="BiliBili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;1327099977"><i class="fa-brands fa-bilibili fa-fw"></i>BiliBili</span>
      </span>
      <span class="links-of-author-item">
        <a href="/categories/Love/" title="Love → &#x2F;categories&#x2F;Love&#x2F;" rel="noopener me"><i class="fa fa-heart fa-fw"></i>Love</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ethanliu6.github.io/2024/04/25/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day08/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day08-%E5%BC%82%E5%B8%B8,Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F,Stream%E6%B5%81%20/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Ethan Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="<font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="<font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font>">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JavaSE进阶——Day08 异常、Lambda表达式、Stream流、File类、递归 | <font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaSE进阶——Day08 异常、Lambda表达式、Stream流、File类、递归<span class="exturl post-edit-link" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni9ldGhhbmxpdTYuZ2l0aHViLmlvL3RyZWUvbWFzdGVyL19wb3N0cy9KYXZhTm90ZXMvSmF2YVNF6L+b6Zi2L2RheTA4L0phdmFTRei/m+mYtuKAlOKAlERheTA4LeW8guW4uCxMYW1iZGHooajovr7lvI8sU3RyZWFt5rWBIC5tZA==" title="编辑"><i class="fa fa-pen-nib"></i></span>
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-04-25 01:36:57" itemprop="dateCreated datePublished" datetime="2024-04-25T01:36:57+08:00">2024-04-25</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-04-27 10:55:51" itemprop="dateModified" datetime="2024-04-27T10:55:51+08:00">2024-04-27</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java%E5%85%A8%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">Java全栈</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    <a title="JavaSE进阶——Day08 异常、Lambda表达式、Stream流、File类、递归" href="/2024/04/25/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day08/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day08-%E5%BC%82%E5%B8%B8,Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F,Stream%E6%B5%81%20/#SOHUCS" itemprop="discussionUrl">
      <span id="sourceId::01d32ec82ccb9c2ab45d07e4306923e2" class="cy_cmt_count" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>1.4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>5 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote class="blockquote-center">
<p>JavaSE进阶——Day08
该篇主要讲解Java中的<strong>异常、Lambda表达式、Stream流、File类、递归</strong></p>

</blockquote>
<span id="more"></span>
<h2 id="回顾">回顾</h2>
<h3 id="logback使用步骤">logback使用步骤：</h3>
<ol type="1">
<li>在当前项目下导入logback相关jar文件，并添加到项目工程资源库中</li>
<li>把logback核心配置文件，复制到当前项目工程的src目录下</li>
<li>在开发的类中，创建logback对象，使用logback中的API方法记录日志信息</li>
</ol>
<h3 id="logback日志级别">logback日志级别：</h3>
<ol type="1">
<li><code>trace</code> //追踪</li>
<li><code>debug</code> //调试（程序有bug，测试bug使用的）</li>
<li><code>info</code> //关键信息</li>
<li><code>warn</code> //警告</li>
<li><code>error</code> //错误</li>
</ol>
<h2 id="异常">异常</h2>
<h3 id="什么是异常">什么是异常？</h3>
<ul>
<li>程序在运行过程中，发生了不正常的情况，造成程序中断向下执行</li>
</ul>
<p>有异常了怎么办呢？</p>
<ul>
<li>当前发生异常的代码位置处，有处理异常的代码，那么异常就会被处理掉，程序继续执行</li>
<li>当前发生异常的代码位置处，没有处理异常的代码(程序中没有处理异常的代码)，最终异常会抛出给JVM来处理
<ul>
<li>JVM处理异常的方式：
<ol type="1">
<li>打印异常的信息（异常的类型、异常发生的位置、异常的原因）</li>
<li>JVM停止</li>
</ol></li>
</ul></li>
</ul>
<h3 id="异常体系">异常体系</h3>
<figure>
<img data-src="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240425014443078.png" alt="image-20240425014443078">
<figcaption aria-hidden="true">image-20240425014443078</figcaption>
</figure>
<h3 id="异常处理的方式">异常处理的方式：</h3>
<ol type="1">
<li>声明：throws
<ul>
<li>遇到异常发生了，自己不处理，交给别人处理
<ul>
<li>最终还是需要有一个位置使用try..catch来处理异常
<ul>
<li>结论：在main方法中，只能使用try..catch</li>
</ul></li>
</ul></li>
</ul></li>
<li>捕获：try..catch
<ul>
<li>遇到异常发生了，自己处理</li>
</ul></li>
</ol>
<p>使用声明的方式处理异常：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用声明的方式处理异常，声明是书写在方法定义上</span></span><br><span class="line">修饰符号  返回值类型 方法名(参数类型 参数,...) <span class="keyword">throws</span> 异常类<span class="number">1</span> , 异常类<span class="number">2</span> ， .... </span><br><span class="line">&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">//在定义方法时，使用声明的方式处理异常</span><br><span class="line">public void method(String name) throws NullPointerException &#123;</span><br><span class="line">   //</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure>
<img data-src="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240425004947611.png" alt="image-20240425004947611">
<figcaption aria-hidden="true">image-20240425004947611</figcaption>
</figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> ex;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.DateFormat;</span><br><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/25 00:52</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 异常类型及处理</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ExceptionDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy/MM/dd&quot;</span>);</span><br><span class="line">        <span class="comment">//1.编译时异常 (编译时异常在编译时就会检查, 所以必须写在方法后面进行显示声明)</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            mothed1(sdf, <span class="string">&quot;2024-04-24&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ParseException e) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;时间格式不正确&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.运行时异常</span></span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">5</span>];</span><br><span class="line">        <span class="type">String</span> <span class="variable">str</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        mothed2(arr, str);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">mothed1</span><span class="params">(SimpleDateFormat sdf, String strDate)</span> <span class="keyword">throws</span> ParseException &#123;</span><br><span class="line">        <span class="comment">//1.编译时异常, 捕获时间解析异常</span></span><br><span class="line">        <span class="comment">//父类: Exception</span></span><br><span class="line">        <span class="comment">//子类: ParseException</span></span><br><span class="line">        System.out.println(sdf.parse(strDate));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">mothed2</span><span class="params">(<span class="type">int</span>[] arr, String str)</span> <span class="keyword">throws</span> RuntimeException &#123;</span><br><span class="line">        <span class="comment">//2.运行时异常</span></span><br><span class="line">        <span class="comment">//子类: ArrayIndexOutOfBoundsException </span></span><br><span class="line">        <span class="comment">//     NullPointerException</span></span><br><span class="line">        <span class="comment">//父类: RuntimeException</span></span><br><span class="line">        System.out.println(str.length() + arr[arr.length]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><font color="red">Java基础面试题 ： throws和throw的区别</font></p>
<div class="note primary"><ul>
<li><p>throws ： 声明</p></li>
<li><p>throw ： 抛出 （手动引发异常）</p>
<ul>
<li>程序员手动创建一个异常对象，并招抛出给调用者</li>
</ul></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//把创建的异常类对象抛出给调用者</span></span><br><span class="line"><span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;参数不能为空&quot;</span>);</span><br></pre></td></tr></table></figure>
</div>
<p><img data-src="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240425161803932-20240425162136434.png"></p>
<p>代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> ex;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/25 01:23</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> throw抛出异常</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ExceptionDemo3</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        getName(name); <span class="comment">//这里需要对抛出的异常进行捕获</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (name == <span class="literal">null</span> || <span class="string">&quot;&quot;</span>.equals(name))&#123;</span><br><span class="line">          <span class="comment">//当前方法不能够解决该异常时候抛出异常</span></span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">&quot;name不能为空&quot;</span>, <span class="keyword">new</span> <span class="title class_">NullPointerException</span>(<span class="string">&quot;空指针异常&quot;</span>));</span><br><span class="line">            <span class="comment">//System.out.println(&quot;dadada&quot;); 不可再写</span></span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;dadada&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>异常处理有两种：声明、捕获，在程序开发中到底怎么选择使用哪个呢？</p>
<ul>
<li><p>自定义方法（程序员自己写的方法），通常都可以使用：声明</p>
<ul>
<li>方法体内代码比较清爽（阅读性好）</li>
<li>把异常统一抛出到main方法中，进行统一的处理</li>
</ul></li>
<li><p>捕获的使用场景：</p>
<ol type="1">
<li><p>main方法中只能使用捕获</p></li>
<li><p>父类型中的方法不支持throws，在子类重写方法时，重写的方法只能使用：捕获</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Demo</span>  <span class="keyword">extends</span> <span class="title class_">Thread</span>&#123;</span><br><span class="line">    <span class="comment">//重写方法</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">run</span><span class="params">()</span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Thread.sleep(<span class="number">100</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol></li>
</ul>
<p>捕获和日志代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> ex;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/25 16:42</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 异常捕获</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ExceptionDemo4</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">Logger</span> <span class="variable">LOGGER</span> <span class="operator">=</span> LoggerFactory.getLogger(<span class="string">&quot;ExceptionDemo4类&quot;</span>);</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">        <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;输入你的生日(年-月-日):&quot;</span>);</span><br><span class="line">            <span class="type">String</span> <span class="variable">birthday</span> <span class="operator">=</span> sc.nextLine();</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                mothed(birthday); <span class="comment">//需要捕获(调用的方法可能发生异常,需要自己处理)</span></span><br><span class="line">                System.out.println(<span class="string">&quot;生日: &quot;</span> + birthday);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (ParseException e) &#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;开始捕获异常&quot;</span>);</span><br><span class="line">                LOGGER.error(e.getMessage());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Date <span class="title function_">mothed</span><span class="params">(String date)</span> <span class="keyword">throws</span> ParseException &#123;</span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy-MM-dd&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> sdf.parse(date); <span class="comment">//需要声明异常</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>Throwable类 //异常的顶层父类型</p>
<ul>
<li><p>子类：Error类（错误类） //异常处理无法解决错误</p></li>
<li><p>子类：Exception类（异常类）//可以使用异常处理解决（保证程序运行过程中不会中断）</p></li>
</ul>
<p>编译时异常：Exception类</p>
<p>运行时异常：RuntimeException类 （继承了Exception）</p>
<h3 id="自定义异常">自定义异常：</h3>
<ul>
<li><p>程序员自己编写的异常类</p></li>
<li><p>解决问题：
JDK提供的异常类在命名上做不到见名其知意，通常在开发中程序员会自定义自己的异常类（见名其知意）</p></li>
<li><p>使用：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> 自定义异常类 <span class="keyword">extends</span> <span class="title class_">Exception</span>&#123; <span class="comment">//当前自定义异常类为：编译时异常</span></span><br><span class="line">    <span class="keyword">public</span> 自定义异常类()&#123;</span><br><span class="line">        <span class="comment">//super();//调用父类中的无参构造方法</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> 自定义异常类(String message)&#123;</span><br><span class="line">        <span class="built_in">super</span>(message);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> 自定义异常类 <span class="keyword">extends</span> <span class="title class_">RuntimeException</span>&#123; <span class="comment">//当前自定义异常类为：运行时异常</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="lambda">Lambda</h2>
<h3 id="lambda作用">lambda作用：</h3>
<ul>
<li>简化程序中的匿名内部类代码书写</li>
</ul>
<h3 id="lambda表达式">lambda表达式：</h3>
<ul>
<li><p>前置要求：仅针对<font color="blue">函数式接口</font>进行代码编写</p>
<ul>
<li>函数式接口的特点：接口中仅有一个抽象方法（允许有：静态方法、默认方法、私有方法）</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FunctionalInterface</span>    <span class="comment">//java针对函数式接口，制定了一个注解：@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">Comparator</span>&lt;T&gt; &#123;</span><br><span class="line">    .......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>标准语法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">(参数 , ....) -&gt; &#123;</span><br><span class="line">    <span class="comment">//方法体代码（要做什么事情）</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>（） ： 代表的是一个方法</li>
<li>-&gt; ： 指向要做的事情</li>
<li>{ } ： 功能代码（具体要做事情的代码）</li>
</ul></li>
</ul>
<p>例：使用Collections对List排序,
要求使用比较器实现降序,用Lambda简化</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> lam;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Collections;</span><br><span class="line"><span class="keyword">import</span> java.util.Comparator;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/25 22:15</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 使用Collections对List排序, 要求使用比较器实现降序,用Lambda简化</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">LambdaDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        List&lt;Integer&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        Collections.addAll(list, <span class="number">4</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">8</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//1.之前写法</span></span><br><span class="line"><span class="comment">//        Collections.sort(list, new Comparator&lt;Integer&gt;() &#123;</span></span><br><span class="line"><span class="comment">//            @Override</span></span><br><span class="line"><span class="comment">//            public int compare(Integer n1, Integer n2) &#123;</span></span><br><span class="line"><span class="comment">//                return n2 - n1;</span></span><br><span class="line"><span class="comment">//            &#125;</span></span><br><span class="line"><span class="comment">//        &#125;);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//2.使用Lambda表达式</span></span><br><span class="line">        Collections.sort(list, (Integer n1, Integer n2) -&gt; &#123;</span><br><span class="line">            <span class="keyword">return</span> n2 - n1;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        System.out.println(list);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>例：带参和返回值</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">//Calculator接口</span><br><span class="line">package lam;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * @Author EthanLiu 16693226842@163.com</span><br><span class="line"> * @Date 2024/4/26 00:23</span><br><span class="line"> * @Project JavaCode_SE_Advance</span><br><span class="line"> * @Theme 代参数与返回值的接口方法</span><br><span class="line"> */</span><br><span class="line">//函数式接口(计算器)</span><br><span class="line">@FunctionalInterface</span><br><span class="line">public interface Calculator &#123;</span><br><span class="line">    public abstract int calculator(int n1, int n2);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//CalculatorDemo类</span></span><br><span class="line"><span class="keyword">package</span> lam;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/26 00:26</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> 带参数与返回值的Lambda</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CalculatorDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        addNum((<span class="type">int</span> n1, <span class="type">int</span> n2) -&gt; &#123;</span><br><span class="line">            <span class="keyword">return</span> n1 + n2;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">addNum</span><span class="params">(Calculator calculator)</span> &#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">res</span> <span class="operator">=</span> calculator.calculator(<span class="number">10</span>, <span class="number">20</span>);</span><br><span class="line">        System.out.println(res);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="lambda表达式的省略模式">Lambda表达式的省略模式：</h3>
<ol type="1">
<li>可以省略参数类型：要么全部省略，要么全部保留</li>
<li>如果参数仅有一个时，可以省略小括号</li>
<li>如果代码块中仅有一行代码，可以省略：大括号、分号、return</li>
</ol>
<h2 id="stream流">Stream流</h2>
<h3 id="stream流的作用">stream流的作用：</h3>
<ul>
<li>针对集合进行功能简化开发</li>
</ul>
<p>Stream流的使用通常需要Lambda表达式</p>
<h3 id="stream流方法分类">Stream流方法分类：</h3>
<ol type="1">
<li>获取方法：获取流（创建一个流水线）</li>
<li>中间方法：在流水线进行操作（例：过滤、截取）</li>
<li>终结方法：流水线上的操作结束了，要关闭流水线</li>
</ol>
<h4 id="获取stream流对象">获取Stream流对象：</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//单列集合：Collection[ List、Set ]</span></span><br><span class="line">Stream 流对象 = 单列集合对象.stream();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//双列集合：Map （不能直接获取流对象）</span></span><br><span class="line"><span class="number">1.</span> 先通过keySet()或entrySet()，获取到Set集合</span><br><span class="line"><span class="number">2.</span> Stream  流对象 = Set集合对象.stream();    </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//数组</span></span><br><span class="line">Stream 流对象 = Arrays.stream( 数组 );</span><br><span class="line"></span><br><span class="line"><span class="comment">//特殊的操作： Stream流中的静态方法：  static&lt;T&gt; Stream&lt;T&gt;  of( T... args )</span></span><br><span class="line">Stream 流对象 = Stream.of( 同一种类型元素 ,同一种类型元素,同一种类型元素, ... );</span><br></pre></td></tr></table></figure>
<p>代码案例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> stream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/26 20:45</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> Stream流</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Stream流中三类方法之一 :  获取方法</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    1 单列集合</span></span><br><span class="line"><span class="comment">       可以使用Collection接口中的默认方法stream()生成流</span></span><br><span class="line"><span class="comment">       default Stream&lt;E&gt; stream()</span></span><br><span class="line"><span class="comment">    2 双列集合</span></span><br><span class="line"><span class="comment">        双列集合不能直接获取 , 需要间接的生成流</span></span><br><span class="line"><span class="comment">        可以先通过keySet或者entrySet获取一个Set集合，再获取Stream流</span></span><br><span class="line"><span class="comment">    3 数组</span></span><br><span class="line"><span class="comment">       Arrays中的静态方法stream生成流</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">// 单列集合的获取</span></span><br><span class="line">        method1();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 双列集合的获取</span></span><br><span class="line">        System.out.println();</span><br><span class="line">        method2();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 数组获取</span></span><br><span class="line">        System.out.println();</span><br><span class="line">        method3();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method2</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="comment">//注意,双列集合需要用特有方法</span></span><br><span class="line">        Map&lt;Integer, String&gt; map = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line">        map.put(<span class="number">1</span>, <span class="string">&quot;Java基础&quot;</span>);</span><br><span class="line">        map.put(<span class="number">2</span>, <span class="string">&quot;Java进阶&quot;</span>);</span><br><span class="line">        map.put(<span class="number">3</span>, <span class="string">&quot;MySQL&quot;</span>);</span><br><span class="line">        map.put(<span class="number">4</span>, <span class="string">&quot;JavaWeb&quot;</span>);</span><br><span class="line">        map.put(<span class="number">5</span>, <span class="string">&quot;Spring&quot;</span>);</span><br><span class="line">        map.entrySet().stream().forEach(s -&gt; System.out.print(s + <span class="string">&quot;\t&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">()</span> &#123;</span><br><span class="line">        List&lt;Integer&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        Collections.addAll(list, <span class="number">1</span>, <span class="number">3</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">7</span>, <span class="number">3</span>);</span><br><span class="line">        list.stream().forEach(s-&gt; System.out.print(s + <span class="string">&quot;\t&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method3</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr = &#123;<span class="number">2</span>, <span class="number">74</span>, <span class="number">342</span>, <span class="number">42</span>, <span class="number">23</span>, <span class="number">5</span>&#125;;</span><br><span class="line">        Arrays.stream(arr).forEach(s -&gt; System.out.print(s + <span class="string">&quot;\t&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="中间方法">中间方法：</h4>
<figure>
<img data-src="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240426142645400.png" alt="image-20240426142645400">
<figcaption aria-hidden="true">image-20240426142645400</figcaption>
</figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> stream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Comparator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span> EthanLiu 16693226842@163.com</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Date</span> 2024/4/26 21:35</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Project</span> JavaCode_SE_Advance</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Theme</span> Stream流 中间方法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Stream流中三类方法之一 :  中间方法</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    1 Stream&lt;T&gt; filter(Predicate predicate)：用于对流中的数据进行过滤</span></span><br><span class="line"><span class="comment">        Predicate接口中的方法 : boolean test(T t)：对给定的参数进行判断，返回一个布尔值</span></span><br><span class="line"><span class="comment">    2 Stream&lt;T&gt; limit(long maxSize)：截取指定参数个数的数据</span></span><br><span class="line"><span class="comment">    3 Stream&lt;T&gt; skip(long n)：跳过指定参数个数的数据</span></span><br><span class="line"><span class="comment">    4 static &lt;T&gt; Stream&lt;T&gt; concat(Stream a, Stream b)：合并a和b两个流为一个流</span></span><br><span class="line"><span class="comment">    5 Stream&lt;T&gt; distinct()：去除流中重复的元素。依赖(hashCode和equals方法)</span></span><br><span class="line"><span class="comment">    6 Stream&lt;T&gt; sorted () : 将流中元素按照自然排序的规则排序</span></span><br><span class="line"><span class="comment">    7 Stream&lt;T&gt; sorted (Comparator&lt;? super T&gt; comparator) : 将流中元素按照自定义比较器规则排序</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        list.add(<span class="string">&quot;张无忌&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张翠山&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张三丰&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;谢广坤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;谢广坤&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;赵四&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;刘能&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;小沈阳&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张良&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张良&quot;</span>);</span><br><span class="line"></span><br><span class="line">        list.stream()</span><br><span class="line">                .filter(name-&gt;name.startsWith(<span class="string">&quot;谢&quot;</span>)) <span class="comment">//筛选器, 按照条件筛选</span></span><br><span class="line">                .forEach(-&gt; System.out.println(s));</span><br><span class="line">        System.out.println();</span><br><span class="line">        list.stream().limit(<span class="number">5</span>).forEach(s-&gt; System.out.println(s)); <span class="comment">//只筛选限制的前n个</span></span><br><span class="line">        System.out.println();</span><br><span class="line">        list.stream().skip(<span class="number">3</span>).forEach(s-&gt; System.out.println(s)); <span class="comment">//跳过前n个</span></span><br><span class="line">        System.out.println();</span><br><span class="line">        list.stream().distinct().forEach(s-&gt; System.out.println(s)); <span class="comment">//去重</span></span><br><span class="line">        System.out.println();</span><br><span class="line">        list.stream().sorted().forEach(s -&gt; System.out.println(s)); <span class="comment">//自然排序</span></span><br><span class="line">        System.out.println();</span><br><span class="line">        list.stream()</span><br><span class="line">                .sorted((str1, str2)-&gt; str1.length() - str2.length()) <span class="comment">//重写的Lambda比较器</span></span><br><span class="line">                .forEach(s -&gt; System.out.println(s));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//回顾往期比较器</span></span><br><span class="line"><span class="comment">//        list.stream().sorted(new Comparator&lt;String&gt;() &#123;</span></span><br><span class="line"><span class="comment">//            @Override</span></span><br><span class="line"><span class="comment">//            public int compare(String str1, String str2) &#123;</span></span><br><span class="line"><span class="comment">//                int res = str1.length() - str2.length();</span></span><br><span class="line"><span class="comment">//                if (res == 0)&#123;</span></span><br><span class="line"><span class="comment">//                    res = str1.compareTo(str2);</span></span><br><span class="line"><span class="comment">//                &#125;</span></span><br><span class="line"><span class="comment">//                return res;</span></span><br><span class="line"><span class="comment">//            &#125;</span></span><br><span class="line"><span class="comment">//        &#125;);</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="终结方法">终结方法：</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> stream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.function.Consumer;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Stream流中三类方法之一 :  终结方法</span></span><br><span class="line"><span class="comment">    1 void forEach(Consumer action)：对此流的每个元素执行操作</span></span><br><span class="line"><span class="comment">        Consumer接口中的方法 void accept(T t)：对给定的参数执行此操作</span></span><br><span class="line"><span class="comment">    2 long count()：返回此流中的元素数</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        list.add(<span class="string">&quot;张无忌&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张翠山&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;张三丰&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;谢广坤&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// long count()：返回此流中的元素数,终结方法，流不可再用，需重新创建流对象</span></span><br><span class="line">        <span class="type">long</span> <span class="variable">count</span> <span class="operator">=</span> list.stream().count();</span><br><span class="line">        System.out.println(count);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// void forEach(Consumer action)：对此流的每个元素执行操作</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">method1</span><span class="params">(ArrayList&lt;String&gt; list)</span> &#123;</span><br><span class="line">        <span class="comment">// 把list集合中的元素放在stream流中</span></span><br><span class="line">        <span class="comment">// forEach方法会循环遍历流中的数据</span></span><br><span class="line">        <span class="comment">// 并循环调用accept方法 , 把数据传给s</span></span><br><span class="line">        <span class="comment">// 所以s就代表的是流中的每一个数据</span></span><br><span class="line">        <span class="comment">// 我们只要在accept方法中对数据做业务逻辑处理即可</span></span><br><span class="line">        list.stream().forEach(<span class="keyword">new</span> <span class="title class_">Consumer</span>&lt;String&gt;() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">accept</span><span class="params">(String s)</span> &#123;</span><br><span class="line">                System.out.println(s);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;=====================&quot;</span>);</span><br><span class="line"></span><br><span class="line">        list.stream().forEach( (String s) -&gt; &#123;</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;=====================&quot;</span>);</span><br><span class="line"></span><br><span class="line">        list.stream().forEach(  s -&gt; &#123; System.out.println(s); &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="stream流转换">Stream流转换</h3>
<h3 id="stream流收集方法">Stream流收集方法</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> stream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Stream流的收集操作 : 第一部分</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    需求：过滤元素并遍历集合</span></span><br><span class="line"><span class="comment">        定义一个集合，并添加一些整数1，2，3，4，5，6，7，8，9，10</span></span><br><span class="line"><span class="comment">        将集合中的奇数删除，只保留偶数。</span></span><br><span class="line"><span class="comment">        遍历集合得到2，4，6，8，10</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    结论：在Stream流中无法直接修改集合,数组等数据源中的数据。</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo5</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;Integer&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt;= <span class="number">10</span>; i++) &#123;</span><br><span class="line">            list.add(i);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        list.stream().filter(num -&gt; num % <span class="number">2</span> == <span class="number">0</span>).forEach(num -&gt; System.out.println(num));</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;=============&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 结论：在Stream流中无法直接修改集合,数组中的数据。</span></span><br><span class="line">        System.out.println(list);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>结论：在Stream流中无法直接修改集合,数组中的数据</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> stream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Set;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Collectors;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    Stream流的收集操作 : 第二部分</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    使用Stream流的方式操作完毕之后，我想把流中的数据起来，该怎么办呢？</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    Stream流的收集方法</span></span><br><span class="line"><span class="comment">    R collect(Collector collector) : 此方法只负责收集流中的数据 , 创建集合添加数据动作需要依赖于参数</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    工具类Collectors提供了具体的收集方式</span></span><br><span class="line"><span class="comment">    public static &lt;T&gt; Collector toList()：把元素收集到List集合中</span></span><br><span class="line"><span class="comment">    public static &lt;T&gt; Collector toSet()：把元素收集到Set集合中</span></span><br><span class="line"><span class="comment">    public static  Collector toMap(Function keyMapper,Function valueMapper)：把元素收集到Map集合中</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    需求 :</span></span><br><span class="line"><span class="comment">        定义一个集合，并添加一些整数1，2，3，4，5，6，7，8，9，10</span></span><br><span class="line"><span class="comment">        将集合中的奇数删除，只保留偶数。</span></span><br><span class="line"><span class="comment">        遍历集合得到2，4，6，8，10</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo6</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;Integer&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i &lt;= <span class="number">10</span>; i++) &#123;</span><br><span class="line">            list.add(i);</span><br><span class="line">        &#125;</span><br><span class="line">        list.add(<span class="number">10</span>);</span><br><span class="line">        list.add(<span class="number">10</span>);</span><br><span class="line">        list.add(<span class="number">10</span>);</span><br><span class="line">        list.add(<span class="number">10</span>);</span><br><span class="line">        list.add(<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// collect只负责收集流中的数据</span></span><br><span class="line">        <span class="comment">// Collectors.toList()会负责在底层创建list集合 ,并把数据添加到集合中 , 返回集合对象</span></span><br><span class="line">        List&lt;Integer&gt; list2 = list.stream().filter(num -&gt; num % <span class="number">2</span> == <span class="number">0</span> ).collect(Collectors.toList());</span><br><span class="line">        System.out.println(list2);</span><br><span class="line"></span><br><span class="line">        Set&lt;Integer&gt; set = list.stream().filter(num -&gt; num % <span class="number">2</span> == <span class="number">0</span> ).collect(Collectors.toSet());</span><br><span class="line">        System.out.println(set);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure>
<img data-src="https://cdn.jsdelivr.net/gh/EthanLiu6/ImgsForMe/blog_images/image-20240427104636356.png" alt="image-20240427104636356">
<figcaption aria-hidden="true">image-20240427104636356</figcaption>
</figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> stream_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"><span class="keyword">import</span> java.util.stream.Collectors;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    Stream流的收集操作 : 第三部分</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    1 创建一个ArrayList集合，并添加以下字符串。字符串中前面是姓名，后面是年龄</span></span><br><span class="line"><span class="comment">        &quot;zhangsan,23&quot;</span></span><br><span class="line"><span class="comment">        &quot;lisi,24&quot;</span></span><br><span class="line"><span class="comment">        &quot;wangwu,25&quot;</span></span><br><span class="line"><span class="comment">    2 保留年龄大于等于24岁的人，并将结果收集到Map集合中，姓名为键，年龄为值</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    收集方法 :</span></span><br><span class="line"><span class="comment">    public static  Collector toMap(Function keyMapper  ,   Function valueMapper)：把元素收集到Map集合中</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StreamDemo7</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        ArrayList&lt;String&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        list.add(<span class="string">&quot;zhangsan,23&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;lisi,24&quot;</span>);</span><br><span class="line">        list.add(<span class="string">&quot;wangwu,25&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// collect 只负责收集数据</span></span><br><span class="line">        <span class="comment">// Collectors.toMap负责在底层创建集合对象 , 添加元素</span></span><br><span class="line">        <span class="comment">// toMap方法中的s就是代表的是集合中的每一个元素</span></span><br><span class="line">        <span class="comment">// 第一个参数 : 如何获取map集合中的键</span></span><br><span class="line">        <span class="comment">// 第二个参数 : 如何获取map集合中的值</span></span><br><span class="line">        Map&lt;String, String&gt; map = list.stream().filter(s -&gt; Integer.parseInt(s.split(<span class="string">&quot;,&quot;</span>)[<span class="number">1</span>]) &gt; <span class="number">23</span>).collect(</span><br><span class="line">                Collectors.toMap(</span><br><span class="line">                        (String s) -&gt; &#123;</span><br><span class="line">                            <span class="keyword">return</span> s.split(<span class="string">&quot;,&quot;</span>)[<span class="number">0</span>];</span><br><span class="line">                        &#125;</span><br><span class="line">                        ,</span><br><span class="line">                        (String s) -&gt; &#123;</span><br><span class="line">                            <span class="keyword">return</span> s.split(<span class="string">&quot;,&quot;</span>)[<span class="number">1</span>];</span><br><span class="line">                        &#125;</span><br><span class="line">                )</span><br><span class="line">        );</span><br><span class="line">        System.out.println(map);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="optional类判空">Optional类判空</h2>
<ul>
<li>jdk统一非空对象的判断的工具类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">boolean</span> <span class="variable">res</span> <span class="operator">=</span> Optional.ofNullable(对象).is对象所属类();</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//拆分</span></span><br><span class="line">Optional&lt;类&gt; optional对象 = Optional.ofNullable(对象)</span><br><span class="line">  <span class="keyword">if</span> (optional对象.is对象所属类())&#123;</span><br><span class="line">    ....</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    ....</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="file类">File类</h2>
<h3 id="file类的介绍">File类的介绍</h3>
<ul>
<li>java.io.File
类是文件和目录路径名的抽象表示，主要用于文件和目录的创建、查找和删除等操作</li>
</ul>
<h3 id="构造方法">构造方法</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> file_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    File：它是文件和目录路径名的抽象表示</span></span><br><span class="line"><span class="comment">        文件和目录可以通过File封装成对象</span></span><br><span class="line"><span class="comment">        File封装的对象仅仅是一个路径名。它可以是存在的，也可以是不存在的。</span></span><br><span class="line"><span class="comment">    构造方法 :</span></span><br><span class="line"><span class="comment">        1 File(String pathname)   通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例</span></span><br><span class="line"><span class="comment">        2 File(String parent, String child)   从父路径名字符串和子路径名字符串创建新的 File实例</span></span><br><span class="line"><span class="comment">        3 File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo1</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"><span class="comment">//        1 File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;~/Desktop/abc.txt&quot;</span>);</span><br><span class="line">        System.out.println(f1);</span><br><span class="line"><span class="comment">//        2 File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;~/Desktop/aaa&quot;</span>, <span class="string">&quot;bbb.txt&quot;</span>);</span><br><span class="line">        System.out.println(f2);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        3 File(File parent, String child)   从父抽象路径名和子路径名字符串创建新的 File实例</span></span><br><span class="line">        <span class="type">File</span> <span class="variable">f3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="keyword">new</span> <span class="title class_">File</span>(~/Desktop/aaa<span class="string">&quot;), &quot;</span>bbb.txt<span class="string">&quot;);</span></span><br><span class="line"><span class="string">        System.out.println(f3);</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>
<h3 id="file类的创建功能">File类的创建功能</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> file_demo;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    File类的创建功能 :</span></span><br><span class="line"><span class="comment">        1 public boolean createNewFile()    : 创建一个新的空的文件</span></span><br><span class="line"><span class="comment">        2 public boolean mkdir()   : 创建一个单级文件夹</span></span><br><span class="line"><span class="comment">        3 public boolean mkdirs() : 创建一个多级文件夹</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">File</span> <span class="variable">f1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;~/Desktop/a.txt&quot;</span>);</span><br><span class="line">        <span class="comment">// 1 public boolean createNewFile()     : 创建一个新的空的文件</span></span><br><span class="line">        System.out.println(f1.createNewFile());</span><br><span class="line"></span><br><span class="line">        <span class="type">File</span> <span class="variable">f2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;day10_demo/aaa&quot;</span>);</span><br><span class="line">        <span class="comment">//  2 public boolean mkdir()   : 创建一个单级文件夹</span></span><br><span class="line">        System.out.println(f2.mkdir());</span><br><span class="line"></span><br><span class="line">        <span class="type">File</span> <span class="variable">f3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;day10_demo/bbb/ccc&quot;</span>);</span><br><span class="line">        <span class="comment">// 3 public boolean mkdirs() : 创建一个多级文件夹</span></span><br><span class="line">        System.out.println(f3.mkdirs());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="file类的删除功能">File类的删除功能</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> file_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    File类删除功能 :</span></span><br><span class="line"><span class="comment">        public boolean delete()    删除由此抽象路径名表示的文件或目录</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        注意 : </span></span><br><span class="line"><span class="comment">            1 delete方法直接删除不走回收站。</span></span><br><span class="line"><span class="comment">            2 如果删除的是一个文件，直接删除。</span></span><br><span class="line"><span class="comment">            3 如果删除的是一个文件夹，需要先删除文件夹中的内容，最后才能删除文件夹</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo3</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">File</span> <span class="variable">f1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;~/Desktop/a.txt&quot;</span>);</span><br><span class="line">        <span class="comment">// 1 public boolean createNewFile()     : 创建一个新的空的文件</span></span><br><span class="line">        System.out.println(f1.delete());</span><br><span class="line"></span><br><span class="line">        <span class="type">File</span> <span class="variable">f2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;day10_demo/aaa&quot;</span>);</span><br><span class="line">        <span class="comment">//  2 public boolean mkdir()   : 创建一个单级文件夹</span></span><br><span class="line">        System.out.println(f2.delete());</span><br><span class="line"></span><br><span class="line">        <span class="type">File</span> <span class="variable">f3</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;day10_demo/bbb&quot;</span>);</span><br><span class="line">        <span class="comment">// 3 public boolean mkdirs() : 创建一个多级文件夹</span></span><br><span class="line">        System.out.println(f3.delete());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="file类的判断和获取功能">File类的判断和获取功能</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> file_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    File类判断和获取功能</span></span><br><span class="line"><span class="comment">        public boolean isDirectory()   测试此抽象路径名表示的File是否为目录</span></span><br><span class="line"><span class="comment">        public boolean isFile()    测试此抽象路径名表示的File是否为文件</span></span><br><span class="line"><span class="comment">        public boolean exists()    测试此抽象路径名表示的File是否存在</span></span><br><span class="line"><span class="comment">        public String getAbsolutePath()    返回此抽象路径名的绝对路径名字符串</span></span><br><span class="line"><span class="comment">        public String getPath()    将此抽象路径名转换为路径名字符串</span></span><br><span class="line"><span class="comment">        public String getName()    返回由此抽象路径名表示的文件或目录的名称</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo4</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">File</span> <span class="variable">f1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;day10_demo/aaa&quot;</span>);</span><br><span class="line">        <span class="type">File</span> <span class="variable">f2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;day10_demo/a.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public boolean isDirectory() 测试此抽象路径名表示的File是否为目录</span></span><br><span class="line">        System.out.println(f1.isDirectory());</span><br><span class="line">        System.out.println(f2.isDirectory());</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public boolean isFile()  测试此抽象路径名表示的File是否为文件</span></span><br><span class="line">        System.out.println(f1.isFile());</span><br><span class="line">        System.out.println(f2.isFile());</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public boolean exists()  测试此抽象路径名表示的File是否存在</span></span><br><span class="line">        System.out.println(f1.exists());</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public String getAbsolutePath()  返回此抽象路径名的绝对路径名字符串</span></span><br><span class="line">        System.out.println(f1.getAbsolutePath());</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public String getPath()  将此抽象路径名转换为路径名字符串</span></span><br><span class="line">        System.out.println(f1.getPath());</span><br><span class="line"></span><br><span class="line"><span class="comment">//        public String getName()  返回由此抽象路径名表示的文件或目录的名称</span></span><br><span class="line">        System.out.println(f2.getName());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="file类高级获取功能">File类高级获取功能</h3>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> file_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    File类高级获取功能</span></span><br><span class="line"><span class="comment">        public File[] listFiles()  返回此抽象路径名表示的目录中的文件和目录的File对象数组</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    listFiles方法注意事项：</span></span><br><span class="line"><span class="comment">        1 当调用者不存在时，返回null</span></span><br><span class="line"><span class="comment">        2 当调用者是一个文件时，返回null</span></span><br><span class="line"><span class="comment">        3 当调用者是一个空文件夹时，返回一个长度为0的数组</span></span><br><span class="line"><span class="comment">        4 当调用者是一个有内容的文件夹时，将里面所有文件和文件夹的路径放在File数组中返回</span></span><br><span class="line"><span class="comment">        5 当调用者是一个有隐藏文件的文件夹时，将里面所有文件和文件夹的路径放在File数组中返回，包含隐藏内容</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileDemo5</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;day10_demo/bbb/ccc&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 返回此抽象路径名表示的目录中的文件和目录的File对象数组</span></span><br><span class="line">        File[] files = file.listFiles();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 遍历File类的数组</span></span><br><span class="line">        <span class="keyword">for</span> (File f : files) &#123;</span><br><span class="line">            <span class="comment">// 拿到每一个文件的文字</span></span><br><span class="line">            System.out.println(f.getName());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="练习">练习 :</h4>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> file_demo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    练习二 ：统计一个文件夹中每种文件的个数并打印。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">   打印格式如下：</span></span><br><span class="line"><span class="comment">      txt:3个</span></span><br><span class="line"><span class="comment">      doc:4个</span></span><br><span class="line"><span class="comment">      jpg:6个</span></span><br><span class="line"><span class="comment">      …</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">FileTest2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">File</span> <span class="variable">file</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">File</span>(<span class="string">&quot;day10_demo/统计文件个数文件夹&quot;</span>);</span><br><span class="line"></span><br><span class="line">        getFileCount(file);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getFileCount</span><span class="params">(File f)</span> &#123;</span><br><span class="line"></span><br><span class="line">        HashMap&lt;String, Integer&gt; hm = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 是文件夹在获取所有的子文件</span></span><br><span class="line">        <span class="keyword">if</span> (f.isDirectory()) &#123;</span><br><span class="line">            File[] files = f.listFiles();</span><br><span class="line">            <span class="comment">// 遍历数组</span></span><br><span class="line">            <span class="keyword">for</span> (File file : files) &#123;</span><br><span class="line">                <span class="type">String</span> <span class="variable">fileName</span> <span class="operator">=</span> file.getName();</span><br><span class="line">                <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> fileName.split(<span class="string">&quot;\\.&quot;</span>)[<span class="number">1</span>];</span><br><span class="line">                <span class="keyword">if</span> (hm.containsKey(name)) &#123;</span><br><span class="line">                    hm.put(name, hm.get(name));</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    hm.put(name, <span class="number">1</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        System.out.println(hm);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="递归">递归</h2>
<h3 id="概述">概述</h3>
<ul>
<li>递归概述：以编程的角度来看，递归指的是方法定义中调用方法本身的现象</li>
</ul>
<h3 id="好处">好处</h3>
<ul>
<li>把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解
小的问题解决 , 大的问题也会逐一进行解决</li>
</ul>
<h3 id="注意">注意</h3>
<ul>
<li>递归出口：否则会出现内存溢出</li>
<li>递归规则：需要找到与原问题相似的规模较小的问题</li>
</ul>
<h3 id="案例">案例</h3>
<ul>
<li><p>```java package com.itheima.recursion_demo;</p>
<p>/* 需求：用递归求5的阶乘，并把结果在控制台输出</p>
<pre><code>    思路：
        1 定义一个方法，用于递归求阶乘，参数为一个int类型的变量
        2 在方法内部判断该变量的值是否是1
            是：返回1
            不是：返回n*(n-1)!
        3 调用方法
        4 输出结果</code></pre>
<p>*/ public class Demo1 { public static void main(String[] args) { int
result = jc(5); System.out.println("5的阶乘是:" + result); }</p>
<pre><code>private static int jc(int n) &#123;
    if (n == 1) &#123;
        return 1;
    &#125;
    return n * jc(n - 1);
&#125;</code></pre>
<p>} <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">- ```java</span><br><span class="line">  package com.itheima.recursion_demo;</span><br><span class="line">  </span><br><span class="line">  import java.io.File;</span><br><span class="line">  </span><br><span class="line">  /*</span><br><span class="line">      需求 : 使用递归删除计算机中指定的文件夹</span><br><span class="line">  </span><br><span class="line">      删除D盘中的aaa文件夹!</span><br><span class="line">  </span><br><span class="line">   */</span><br><span class="line">  public class Demo2 &#123;</span><br><span class="line">      public static void main(String[] args) &#123;</span><br><span class="line">          File f = new File(&quot;D:\\aaa&quot;);</span><br><span class="line">          deleteFiles(f);</span><br><span class="line">      &#125;</span><br><span class="line">  </span><br><span class="line">      private static void deleteFiles(File f) &#123;</span><br><span class="line">  </span><br><span class="line">          File[] files = f.listFiles();</span><br><span class="line">  </span><br><span class="line">          for (File file : files) &#123;</span><br><span class="line">              if (file.isDirectory()) &#123;</span><br><span class="line">                  // 递归</span><br><span class="line">                  deleteFiles(file);</span><br><span class="line">              &#125; else &#123;</span><br><span class="line">                  // 删除文件</span><br><span class="line">                  file.delete();</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;</span><br><span class="line">          // 删除当前文件夹</span><br><span class="line">          f.delete();</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></p></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wChat.png" alt="Ethan Liu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Ethan Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://ethanliu6.github.io/2024/04/25/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day08/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day08-%E5%BC%82%E5%B8%B8,Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F,Stream%E6%B5%81%20/" title="JavaSE进阶——Day08 异常、Lambda表达式、Stream流、File类、递归">http://ethanliu6.github.io/2024/04/25/JavaNotes/JavaSE进阶/day08/JavaSE进阶——Day08-异常,Lambda表达式,Stream流 /</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/tags/Java%E8%BF%9B%E9%98%B6/" rel="tag"><i class="fa fa-tag"></i> Java进阶</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/04/22/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day07/2024-04-19-JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day06-%E6%8E%92%E5%BA%8F%E6%9F%A5%E6%89%BE%E7%AE%97%E6%B3%95%E3%80%81Map%E9%9B%86%E5%90%88%E3%80%81%E9%9B%86%E5%90%88%E5%B5%8C%E5%A5%97%E3%80%81%E6%96%97%E5%9C%B0%E4%B8%BB%E6%A1%88%E4%BE%8B/" rel="prev" title="JavaSE进阶——Day07 集合嵌套、Collections工具类、日志框架">
                  <i class="fa fa-angle-left"></i> JavaSE进阶——Day07 集合嵌套、Collections工具类、日志框架
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2024/04/27/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day09/2024-04-27-JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day09-%E5%A4%9A%E7%BA%BF%E7%A8%8B%EF%BC%88%E4%BA%8C%EF%BC%89/" rel="next" title="JavaSE进阶——Day09 多线程（二）">
                  JavaSE进阶——Day09 多线程（二） <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="SOHUCS" sid="01d32ec82ccb9c2ab45d07e4306923e2"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2023-2024 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Is Ethan's</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">113k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:51</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni8=" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.umd.js" integrity="sha256-+2+qOqR8CKoHh/AsVR9k2qaDBKWjYNC2nozhYmv5j9k=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.1.0/medium-zoom.min.js" integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.12/pdfobject.min.js","integrity":"sha256-g2xji1rlE3KsGVClvuxTbcR0Kn2+wtQADSff2Tbb4zA="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>



  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"all","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":true,"archive":true,"delay":true,"timeout":3000,"priority":true,"url":"http://ethanliu6.github.io/2024/04/25/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day08/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day08-%E5%BC%82%E5%B8%B8,Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F,Stream%E6%B5%81%20/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script class="next-config" data-name="changyan" type="application/json">{"enable":true,"appid":"cyxf6tytI","appkey":"90987f30c4b5248baad5a04c7a6abfe1","count":true}</script>
<script src="/js/third-party/comments/changyan.js"></script>

</body>
</html>
